﻿using Microsoft.AspNetCore.Http;

namespace MelodysDemo.Application.Questionnaires
{
    public class QuestionnaireService : IQuestionnaireService
    {
        readonly IRepository<SurveyQuestionnaire, int> _Questnaire;//问卷
        public QuestionnaireService(IRepository<SurveyQuestionnaire, int> Questnaire)
        {
            _Questnaire = Questnaire;
        }
        //查询
        public async Task<PagedResultDto<SurveyQuestionnaire>> GetResultDto(QuestionnaireInput input)
        {
            var q = _Questnaire.GetAll().Where(x => x.IsDelete == 0);

            //问卷名
            if (input.Title is { Length: > 0 })
            {
                q = q.Where(x => x.Title.Contains(input.Title));
            }
            //状态
            if (input.Status < 3)
            {
                q = q.Where(x => x.Status == input.Status);
            }
            //开始时间
            if (input.Begin is { Length: > 0 })
            {
                q = q.Where(x => x.CreateTime >= DateTime.Parse(input.Begin));
            }
            //结束时间
            if (input.End is { Length: > 0 })
            {
                q = q.Where(x => x.CreateTime <= DateTime.Parse(input.End));
            }

            var count = q.Count();
            var list = await q.OrderByDescending(x => x.Id)
                                               .Skip((input.Page - 1) * input.Limit).Take(input.Limit)
                                               .AsNoTracking().ToListAsync();
            return new PagedResultDto<SurveyQuestionnaire>
            {
                Page = input.Page,
                Limit = input.Limit,
                TotalCount = count,
                Data = list
            };
        }
    }
}
